use clap::{Parser, Subcommand};

#[derive(Debug, Parser)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
    #[command(subcommand)]
    cmd: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    #[command(name = "csv", about = "Generate CSV file")]
    Csv(CsvArgs),
}

#[derive(Debug, Parser)]
pub struct CsvArgs {
    #[arg(short, long, value_parser = value_parse_name, help = "Name of the CSV file")]
    name: String,
    #[arg(short, long, help = "Path to the CSV file")]
    path: String,

    #[arg(
        short,
        long,
        default_value = "fdshlex",
        help = "output to the CSV file"
    )] // default_value 会调用 "fdshlex".into()
    output: Option<String>,
    #[arg(short, long, default_value_t = 1024, help = "size to the CSV file")]
    size: usize,
}

// 必须返回一个能够 Display 的类型
fn value_parse_name(name: &str) -> Result<String, &'static str> {
    if name.is_empty() {
        return Err("Name cannot be empty");
    }
    if name.len() > 10 {
        return Err("Name is too long");
    }
    Ok(name.into())
}
